package com.test07;

import java.util.Random;

/**
 * 2、声明一个身体Body类型
 * （1）包含属性：所有者owner(String)，体重weight(double)（单位：克）,健康状态health(boolean)，心脏heart (Organ类型)
 * （2）声明有参构造，get/set，toString()
 * 其中heart属性在有参构造中根据身体状况创建心脏对象
 * （3）声明一个私有的内部类Heart，继承Organ类，内部类包含属性：心率，颜色，大小（单位：克），
 * 重写doWork()方法：打印“为血液流动提供动力，把血液运行至xx的身体各个部分”。
 * ①其中xx是Body的owner所有者。
 * ②其中心脏大小等于体重的0.5％
 * ③其中心脏颜色，如果人健康，心脏为鲜红色，人不健康，心脏为暗红色
 * ④其中心脏心率，如果人健康，心率为：60~100次/分，如果在此范围之外，则人不健康
 * ⑤重写doWork()方法：
 * 打印“心率：xx次/分，大小：xx克，颜色：xx”
 * 打印“为血液流动提供动力，把血液运行至身体各个部分”
 */
public class Body {
    private String owner;
    private double weight;
    private boolean health;
    private Organ heart;

    public Body(){}
    public Body(String owner, double weight, boolean health) {
        this.owner = owner;
        this.weight = weight;
        this.health = health;
        this.heart = new Heart(weight,health,this);
    }

    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public boolean isHealth() {
        return health;
    }
    public void setHealth(Organ heart) {
        if(heart.getHeartColour().equals("鲜红色") && (heart.getHeartRate() >= 60 && heart.getHeartRate() <= 100)){
            this.health = true;
        }else{
            this.health = false;
        }
    }
    public Organ getHeart() {
        return heart;
    }
    public void setHeart(Organ heart, Body body) {
        this.heart = heart;
        this.heart.setBodyId(body);
    }
    @Override
    public String toString() {
        return "姓名" + owner + ", 体重：" + weight + ", 健康与否：" + (health ? "健康" : "不健康");
    }
    private class Heart extends Organ{
        private int heartRate;
        private String heartColour;
        private double heartWeight;
        private Body bodyId;

        public Heart(){}
        public Heart(double weight, boolean health, Body body){
            setHeartRate(health);
            setHeartColour(health);
            setHeartWeight(weight);
            setBodyId(body);
        }

        public int getHeartRate() {
            return heartRate;
        }
        public void setHeartRate(boolean health) {
            Random random = new Random();
            int r1 = random.nextInt(100) + 101;
            int r2 = random.nextInt(60)+1;
            int r = random.nextInt(1);
            if(r == 0){
                r = r1;
            }else{
                r = r2;
            }
            heartRate = health ? random.nextInt(41) + 60 : r;
        }
        public String getHeartColour() {
            return heartColour;
        }
        public void setHeartColour(boolean health) {
            this.heartColour = health ? "鲜红色" : "暗红色";
        }
        public double getHeartWeight() {
            return heartWeight;
        }
        public void setHeartWeight(double weight) {
            this.heartWeight = weight*0.005;
        }
        public Body getBodyId(){
            return bodyId;
        }
        public void setBodyId(Body body){
            this.bodyId = body;
        }

        @Override
        public void doWork() {
            //System.out.println("心率：" + getHeartRate() + "，心脏大小：" + getHeartWeight() + "，心脏颜色：" + getHeartColour());
            System.out.println("‘"+ Body.this.owner + "’的心脏为血液流动提供动力，把血液运行至‘" + bodyId.getOwner() + "’的身体各个部分");
        }

        @Override
        public String toString() {
            return "心率：" + heartRate + "，心脏大小：" + heartWeight + "，心脏颜色：" + heartColour;
        }
    };
}
